/*
 * Created by JFormDesigner on Sun Jul 05 18:50:47 BST 2009
 */

package gui;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

import recordsDatabase.Category;
import recordsDatabase.CategoryDetails;
import recordsDatabase.ListOfCategories;
import recordsDatabase.Person;
import resources.DateUtilities;
import resources.FileReaderWriter;

/**
 * @author alex ouzounis
 */
public class WindowCategories extends JInternalFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7934222328059892475L;
	private DateUtilities dateTime = new DateUtilities();

	public WindowCategories() {
		initComponents();
		initCateogryList();
	}

	private void initCateogryList() {
		for (Category c : ListOfCategories.getInstance().getCategoriesList()) {
			modelCategory.addElement(c.getCategoryName());
		}
	}

	private void listCategoryValueChanged(ListSelectionEvent e) {
		label3.setText("Person(s)");
		if (modelField!=null) modelField.clear();
		modelFieldInfo.clear();
		for (CategoryDetails cd : ListOfCategories.getInstance().getCategory(
				(String) listCategory.getSelectedValue()).getEntriesList()) {
			modelField.addElement(cd.getField());
		}
	}

	private void listFieldValueChanged(ListSelectionEvent e) {
		label3.setText("Person(s) found: "
				+ ListOfCategories.getInstance().getCategory(
						(String) listCategory.getSelectedValue())
						.getCategoyDetails(
								(String) listField.getSelectedValue())
						.getPersonsList().size());
		if (listField.getSelectedIndex() != -1) {
			modelFieldInfo.clear();
			for (Person p : ListOfCategories.getInstance().getCategory(
					(String) listCategory.getSelectedValue())
					.getCategoyDetails((String) listField.getSelectedValue())
					.getPersonsList()) {
				modelFieldInfo.addElement(p.getRecord("Name")
						.getCategoryDetailsList().get(0).getField());
			}
		}

	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY
		// //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - alex ouzounis
		dialogPane = new JPanel();
		contentPanel = new JPanel();
		splitPane1 = new JSplitPane();
		splitPane2 = new JSplitPane();
		panel1 = new JPanel();
		label1 = new JLabel();
		scrollPane1 = new JScrollPane();
		modelCategory = new DefaultListModel();
		listCategory = new JList(modelCategory);
		panel2 = new JPanel();
		label2 = new JLabel();
		scrollPane2 = new JScrollPane();
		modelField = new DefaultListModel();
		listField = new JList(modelField);
		panel3 = new JPanel();
		label3 = new JLabel();
		scrollPane3 = new JScrollPane();
		modelFieldInfo = new DefaultListModel();
		listFieldInfo = new JList(modelFieldInfo);
		buttonExport = new JButton();

		// ======== this ========
		setVisible(true);
		setTitle("Categories");
		setClosable(true);
		setResizable(true);
		setMaximizable(true);
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		// ======== dialogPane ========
		{
			dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));

			dialogPane.setLayout(new BorderLayout());

			// ======== contentPanel ========
			{
				contentPanel.setLayout(new GridLayout());

				// ======== splitPane1 ========
				{
					splitPane1.setOrientation(JSplitPane.VERTICAL_SPLIT);

					// ======== splitPane2 ========
					{

						// ======== panel1 ========
						{
							panel1.setLayout(new BorderLayout());

							// ---- label1 ----
							label1.setText("Select a Category:");
							panel1.add(label1, BorderLayout.NORTH);

							// ======== scrollPane1 ========
							{

								// ---- listCategory ----
								listCategory
										.addListSelectionListener(new ListSelectionListener() {
											public void valueChanged(
													ListSelectionEvent e) {
												listCategoryValueChanged(e);
											}
										});
								scrollPane1.setViewportView(listCategory);
							}
							panel1.add(scrollPane1, BorderLayout.CENTER);
						}
						splitPane2.setLeftComponent(panel1);

						// ======== panel2 ========
						{
							panel2.setLayout(new BorderLayout());

							// ---- label2 ----
							label2.setText("Select a Field:");
							panel2.add(label2, BorderLayout.NORTH);

							// ======== scrollPane2 ========
							{

								// ---- listField ----
								listField
										.addListSelectionListener(new ListSelectionListener() {
											public void valueChanged(
													ListSelectionEvent e) {
												listFieldValueChanged(e);
											}
										});
								scrollPane2.setViewportView(listField);
							}
							panel2.add(scrollPane2, BorderLayout.CENTER);
						}
						splitPane2.setRightComponent(panel2);
					}
					splitPane1.setTopComponent(splitPane2);

					// ======== panel3 ========
					{
						panel3.setLayout(new BorderLayout());

						// ---- label3 ----
						label3.setText("Person(s):");
						panel3.add(label3, BorderLayout.NORTH);

						// ======== scrollPane3 ========
						{
							scrollPane3.setViewportView(listFieldInfo);
						}
						panel3.add(scrollPane3, BorderLayout.CENTER);
						buttonExport.setText("Export Categories");
						buttonExport.addMouseListener(new MouseAdapter() {
							@Override
							public void mouseClicked(MouseEvent e) {
								ExportButtonMouseClicked(e);
							}
						});
						panel3.add(buttonExport, BorderLayout.SOUTH);

					}
					splitPane1.setBottomComponent(panel3);
				}
				contentPanel.add(splitPane1);
			}
			dialogPane.add(contentPanel, BorderLayout.CENTER);
		}
		contentPane.add(dialogPane, BorderLayout.CENTER);
		// JFormDesigner - End of component initialization
		// //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY
	// //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - alex ouzounis
	private JPanel dialogPane;
	private JPanel contentPanel;
	private JSplitPane splitPane1;
	private JSplitPane splitPane2;
	private JPanel panel1;
	private JLabel label1;
	private JScrollPane scrollPane1;
	private JList listCategory;
	private JPanel panel2;
	private JLabel label2;
	private JScrollPane scrollPane2;
	private JList listField;
	private JPanel panel3;
	private JLabel label3;
	private JScrollPane scrollPane3;
	private JList listFieldInfo;
	private DefaultListModel modelCategory, modelField, modelFieldInfo;
	private JButton buttonExport;

	// JFormDesigner - End of variables declaration //GEN-END:variables

	private void ExportButtonMouseClicked(MouseEvent e) {

		StringBuilder text = new StringBuilder();
		String newLine = "\n";
		text.append("DataMiner Categories Export");
		text.append(newLine);
		text.append(dateTime.getDateTime("yyyy.MMMMM.dd GGG hh:mm aaa"));
		text.append(newLine);
		text.append("===================================================");
		text.append(newLine);
		int i = 1;
		for (Category c : ListOfCategories.getInstance().getCategoriesList()) {
			text.append(newLine);
			text.append("------------");
			text.append(newLine);
			text.append("Category: " + c.getCategoryName());
			text.append(newLine);
			for (CategoryDetails cd : c.getEntriesList()) {
				text.append(newLine);
				text.append("Field: " + cd.getField());
				text.append(newLine+newLine);
				for (Person p : cd.getPersonsList()) {
					text.append("Namme: ");
					text.append(p.getRecord("Name").getCategoryDetailsList()
							.get(0).getField());
					text.append(newLine);
				}

			}
			i++;
		}

		// create a JFileChooser with its appropriate properties
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		int fileVal = fileChooser.showSaveDialog(buttonExport);

		// check if the cancel button was pressed
		if (fileVal == JFileChooser.CANCEL_OPTION) {

		} else {
			// if everything is ok then get the selected file from the user
			File fileName = fileChooser.getSelectedFile();

			// if the file is empty display an error message
			if (fileName == null || fileName.getName().equals("")) {
				JOptionPane.showMessageDialog(buttonExport,
						"Please enter a correct filename.", "Error",
						JOptionPane.ERROR_MESSAGE);
			} else

			// if the path name is OK, call the loader!
			{
				try {
					String newFileName=fileName.getName();
					FileReaderWriter fileWrite = new FileReaderWriter();
					if (!fileName.getName().contains(".txt")){
						newFileName=newFileName+".txt";
					}
					fileWrite.WriteFile(newFileName, text.toString());
					JOptionPane.showMessageDialog(buttonExport,
							"Categories exported.", "Success",
							JOptionPane.INFORMATION_MESSAGE);
				} catch (Exception e1) {// Catch exception if any
					System.err.println("Error: " + e1.getMessage());
				}
			}
		}

	}
}
